/**
 * 
 */
package gpl.scotlandyard.android.controllers;

import gpl.scotlandyard.android.R;
import gpl.scotlandyard.android.activities.PathFinderActivity;
import gpl.scotlandyard.android.activities.SplashActivity;
import gpl.scotlandyard.android.beans.adapters.FugitivePathsList;
import gpl.scotlandyard.android.utils.InputValidator;
import gpl.scotlandyard.exceptions.InvalidUserInputException;
import gpl.scotlandyard.services.PathFinder;

import java.util.LinkedList;

import android.util.Log;
import android.widget.GridView;
import android.widget.Toast;

/** @author norbert */
public class PathFinderController extends BasicController {

  private static final String TAG = PathFinderController.class.toString();

  private static final String SEPARATOR = " ";

  private static final int MIN_POSITION = 1;

  private static final PathFinderController INSTANCE = new PathFinderController();

  private PathFinder pathFinder;

  private PathFinderController() {
  }

  @Override
  public void onViewCreated() {
    Log.d(TAG, "created");
    pathFinder = new PathFinder(getModel());
    GridView gridview = (GridView) getActivity().findViewById(R.id.pathFinder_grid);
    gridview.setAdapter(new FugitivePathsList(getActivity(), R.id.simpleTracker_grid, pathFinder));
  }

  /**
   * @param detIn
   * @param fugIn
   */
  public void searchPaths(String fugIn, String detIn) {
    int maxNode = getModel().getMap().size();
    int fugPos = InputValidator.isValidPosition(fugIn, MIN_POSITION, maxNode);
    if (fugPos == -1) {
      Log.w(TAG, "Fugitive position must be between [" + MIN_POSITION + "," + maxNode + "] : " + fugIn);
      Toast.makeText(getActivity(), "Give fugitive position between [" + MIN_POSITION + "," + maxNode + "]", Toast.LENGTH_SHORT).show();
      return;
    }

    LinkedList<Integer> detPos = new LinkedList<Integer>();
    String[] detInTab = detIn.trim().split(SEPARATOR);
    for (int i = 0; i < detInTab.length; i++) {
      int val = InputValidator.isValidPosition(detInTab[i], MIN_POSITION, maxNode);
      // invalid position
      if (val == -1) {
        Log.w(TAG, "Detective position must be between [" + MIN_POSITION + "," + maxNode + "] : " + detInTab[i]);
        Toast.makeText(getActivity(), "Give detective positions between [" + MIN_POSITION + "," + maxNode + "]", Toast.LENGTH_SHORT).show();
        return;
      }
      // detective position equals fugitive position
      if (val == fugPos) {
        Log.w(TAG, "Detective position must be different of fugitive position : " + fugPos);
        Toast.makeText(getActivity(), "Give detective different position than your's (or you just loose)", Toast.LENGTH_SHORT).show();
        return;
      }
      // give 2 times same position for detectives
      if (detPos.contains(val)) {
        Log.w(TAG, "Detective position (" + val + ") already given in this input : " + detPos);
        Toast.makeText(getActivity(), "Give different detective positions between [" + MIN_POSITION + "," + maxNode + "]",
            Toast.LENGTH_SHORT).show();
        return;
      }
      detPos.add(val);
    }

    try {
      pathFinder.setPlayersPositions(fugPos, detPos);
    } catch (InvalidUserInputException e) {
      int nbDetectives = getModel().getDetectives().size();
      Log.w(TAG, "Detective positions contains less values (" + detPos.size() + ") than detective players (" + nbDetectives + ")");
      Toast
          .makeText(getActivity(), "Give as many positions as detective declared please (waited:" + nbDetectives + ")", Toast.LENGTH_SHORT)
          .show();
      return;
    }
    pathFinder.performDiscover();
  }

  @Override
  public PathFinderActivity getActivity() {
    return (PathFinderActivity) super.getActivity();
  }

  @Override
  public void onBackPressed() {
    onBackPressed(SplashActivity.class);
  }

  /** @return the instance */
  public static PathFinderController getInstance() {
    return INSTANCE;
  }

}
